package com.wanmait.myhouse.mediatorController;

import com.wanmait.myhouse.dao.ContactDAO;
import com.wanmait.myhouse.dao.MediatorDAO;
import com.wanmait.myhouse.dao.UserDAO;
import com.wanmait.myhouse.vo.Contact;

import com.wanmait.myhouse.vo.Mediator;
import com.wanmait.myhouse.vo.Pager;
import com.wanmait.myhouse.vo.User;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("mediator")
public class MediatorFeedbackController {
    @GetMapping("feedback")
    public String feedback(Integer id, Model model,Integer pno){
        ContactDAO contactDAO = new ContactDAO();
        UserDAO userDAO = new UserDAO();
        Pager pager = new Pager();
        if (pno == null){
            pno = 1;
        }
        pager.setPageNO(pno);
        pager.setPageSize(4);
        int total = contactDAO.getTotal(id);
        int pageCount = total/pager.getPageSize()+(total% pager.getPageSize()==0?0:1);
        pager.setPageCount(pageCount);
        model.addAttribute("pager",pager);
        List<Contact> contactList = contactDAO.select(pager,id);
        for (Contact c : contactList){
            User user = userDAO.findById(c.getUserId());
            c.setUser(user);
        }
        model.addAttribute("contactList",contactList);
        return "/mediator/feedback";
    }
    @PostMapping("found")
    public String found(Contact contact, HttpSession session){
        Object o = session.getAttribute("mediator");
        Mediator mediator = (Mediator) o;
        contact.setDispose(true);
        ContactDAO contactDAO = new ContactDAO();
        contactDAO.add(contact);
        return "redirect:/mediator/feedback?id="+mediator.getId();
    }
}
